<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$recipient = trim(UV::readMQ(strip_tags($route->GET("recipient"))));
if ($recipient == $user->getUserId())
	goToPage("/cabinet/messages/recieved");
$who = $db->getRow("select `user_id`,`login`,`sname`,`name` from `users` where `user_id` = '$recipient' limit 1");
if (!$who)
	goToPage("/cabinet/messages/recieved");

if (UV::readMQ($route->POST("act")) == "post")
{
	$sender_id = $user->getUserId();
	$recipient_id = UV::readInt(strip_tags($route->POST("whom")));
	$header = UV::readMQ(trim(strip_tags($route->POST("header"))));
	if ($header == "")
		$header = "Без темы";
	$text = UV::readMQ(trim(strip_tags($route->POST("text"))));
	if ($text == "")
		$text = "Без текста";
	$time = time();
	
	$success = $db->sendQuery("insert into `messages` values (NULL,'$sender_id','$recipient_id','$header','$text','$time',0,1,1)");
}

require_once CABINET.DS."head.php";
?>

<div class="full">
	<a class="submenu_button" href='/cabinet/messages/received'>Полученные</a>
	<a class="submenu_button" href='/cabinet/messages/sended'>Отправленные</a>
	<a class="submenu_button submenu_button_active">Отправить</a><br /><br /><br />
	<h3>Отправить сообщение</h3>
	<?php
	if (isset($success))
	{
		if ($success)
			echo "<p style='color:green'>Ваше сообщение отправлено</p>";
		else
			echo "<p style='color:green'>Ошибка при отправке письма</p>";
	}
	?>
	<form method='post'>
		<input type='hidden' name='act' value='post' />
		<table cellspacing='10'>
			<tr>
				<td>Получатель:</td>
				<td>
					<input name='whom' type='hidden' value="<?php echo $who['user_id']; ?>"  />
					<?php echo $who['name']." ".$who['sname']; ?>
				</td>
			</tr>
			<tr valign='top'>
				<td>Тема:</td>
				<td><input type='text' style="width:300px" name='header' class="rBorder" /></td>
			</tr>
			<tr valign='top'>
				<td>Текст:</td>
				<td><textarea rows='5' style="width:300px" name='text' class="rBorder"></textarea></td>
			</tr>
			<tr>
				<td colspan='2' align='right'><input type='submit' value='Отправить' /></td>
			</tr>
		</table>
	</form>
</div>

<?php require_once CABINET.DS."footer.php"; ?>
